﻿using System;
using System.Collections.Generic;
using System.Transactions;
using Common;
using Dto;
using SqlServerDAL;

namespace Business
{
    public class GroupPhotoBusiness
    {
        private const string Waming = "Business.PhotoBusiness";
        private readonly GroupPhotoDAL groupPhotoDal = new GroupPhotoDAL();
        private readonly GroupAlbumDAL groupAlbumDal = new GroupAlbumDAL();

        /// <summary>
        /// 小组成员上传相片
        /// </summary>
        /// <param name="groupPhotoDtos"></param>
        /// <returns></returns>
        public bool SaveGroupPhoto(List<GroupPhotoDto> groupPhotoDtos)
        {
            var tran = new TransactionScope();
            try
            {
                var annexBusiness = new AnnexBusiness();
                foreach (var dto in groupPhotoDtos)
                {
                    var annex = annexBusiness.AddAnnex(dto.annex);
                    dto.GpAnnexId = annex.AnnexID;
                    groupPhotoDal.Save(dto);
                }
                tran.Complete();
                return true;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".SaveGroupPhoto(List<GroupPhotoDto> groupPhotoDtos)", ex);
                return false;
            }
            finally
            {
                tran.Dispose();
            }
        }

        /// <summary>
        /// 保存小组相册
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public bool SaveGroupAlbum(GroupAlbumDto dto)
        {
            try
            {
                groupAlbumDal.Save(dto);
                return true;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".SaveAlbum(GroupAlbumDto dto)", ex);
                return false;
            }
        }
    }
}